<?php

/* 
 * TAO KANG -- 897637176
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;

class PromotionController extends AdminBaseController{
    public function index(){
        return $this->fetch();
    }
    //促销商品列表
    public function prolist(){
        $keywordComplex = [];
        $request = input('request.');
        if(!empty($request['keyword'])){
            $keyword = $request['keyword'];
            $keywordComplex['store_name|g_goodsname|re_name'] = ['like',"%$keyword%"];
        }
        $adminid = cmf_get_current_admin_id();
        $shopid = Db::name('user')->where('id='.$adminid)->field('store_id')->find();//查询当前用户所在的店铺id
        $list = Db::name('promotion')->where('c_shopid='.implode(" ",$shopid))->whereOr($keywordComplex)->alias('a')->join('__STORE__ b','a.c_shopid=b.id','left')->join('__GOODS__ c','a.c_goodsid=c.g_id')->join('__REBATE__ d','a.c_rebateid=d.re_id')->order('c_id DESC')->field('a.*,b.store_name,c.g_goodsname,d.re_name')->paginate(10);
        $list->appends($request);//保持查询分页不变
        $this->assign('list',$list);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //折扣类型分类列表
    public function rebatelist(){
        $list = Db::name('rebate')->alias('s')->join('__STORE__ a','s.re_shopid=a.id','left')->order('re_id ASC')->field('s.*,a.store_name')->paginate(10);
        $this->assign('list',$list);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //显示折扣类型分类数据
    public function rebateindex(){
        return $this->fetch();
    }
    //添加折扣类型分类数据
    public function rebatePost(){
        if($this->request->isPost()){
            $validate = new Validate([
                're_name' => 'require',
            ]);
            $validate->message([
                're_name.require' => "分类名称不能为空",
            ]);
            $data = $this->request->post();
            if(!$validate->check($data)){
                $this->success($validate->getError());
            }
            $adminid = cmf_get_current_admin_id();
            $shopid = Db::name('user')->where('id='.$adminid)->find();//查询当前管理员的信息的商铺ID
            $datapost = array();
            $datapost['re_name'] = $data['re_name'];
            $datapost['re_shopid'] = $shopid['store_id'];
            $result = Db::name('rebate')->insert($datapost);
            if($result){
                $this->success(lang('EDIT_SUCCESS'), "blog/Promotion/rebateindex");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/rebateindex");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //修改之前，显示要修改的折扣类型分类数据
    public function rebaddlist(){
        $memberid = $this->request->param("nav_id");
        $list = Db::name('rebate')->where('re_id='.$memberid)->field("re_name,re_id")->find();
        $this->assign('list',$list);
        return $this->fetch();   
    }
    //修改折扣类型分类名称，添加数据库
    public function rebaddPost(){
        if($this->request->isPost()){
            $validate = new Validate([
                're_name' => 'require',
            ]);
            $validate->message([
                're_name.require' => "分类名称不能为空",
            ]);
            $data = $this->request->post();
            if(!$validate->check($data)){
                $this->success($validate->getError());
            }
            $datapost = array();
            $datapost['re_name'] = $data['re_name'];
            $adminid = cmf_get_current_admin_id();
            $shopid = Db::name('user')->where('id='.$adminid)->field('store_id')->find();
            $result = Db::name('rebate')->where('re_id='.$data['re_id'])->where('re_shopid='.implode(" ",$shopid))->update($datapost);
            if($result){
                $this->success("修改成功");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/rebateindex");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    
    /*---------------------------------------------------------------------------*/
    //显示促销商品添加页面
    public function zheKou(){
        $list = Db::name('rebate')->field('re_id,re_name')->select();
        $this->assign('list',$list);
        //$listone = Db::name('store_type')->field('id,cat_name')->select(); 
        //$this->assign('listone',$listone);
        return $this->fetch();
    }
    //显示促销规格列表
    public function guige(){
       $intNavId = $this->request->param("nav_id");
       $goodsid = Db::name('promotion')->where('c_id='.$intNavId)->find();
       $list = Db::name("format")->where(array('pid'=>$goodsid['c_goodsid'],'store_type'=>$goodsid['store_type']))->field('id,format,price')->select();
       $this->assign('list',$list);
       $this->assign('intNavId',$intNavId);
       return $this->fetch(); 
    }
    //提交促销规格表数据
    public function guipost(){
        if($this->request->isPost()){
            $data = $this->request->Post();
            $youhuijia = Db::name('promotion')->where('c_id='.$data['formatid'])->find();//优惠价格
            $yuanjia = Db::name('format')->where('id='.$data['c_format'])->find();//原价
            $datame = array();
            $datame['c_price'] = $yuanjia['price'];//原价
            $datame['c_money'] = $yuanjia['price'] - $youhuijia['c_rmb'];
            $datame['c_format'] = $data['c_format'];
            $result = Db::name('promotion')->where('c_id='.$data['formatid'])->update($datame);
            if($result){
                $this->success("添加成功","blog/Promotion/prolist");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/guige");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //添加促销商品数据
    public function zhePost(){
        if($this->request->isPost()){
            $data = $this->request->Post();
            $adminid = cmf_get_current_admin_id();
            $shopid = Db::name('user')->where('id='.$adminid)->field('store_id')->find();//查询当前用户所在的店铺id
            //$yuanjia = Db::name('promotion')->where('c_goodsid='.$data['proid'])->alias('a')->join('__FORMAT__ b','a.c_format=b.id','left')->field('a.*,b.price')->order('c_starttime DESC')->select();//商品的原价
            $yuanjia = Db::name("goods")->where('g_id='.$data['proid'])->find();
            $dataone = array();
            $rand = rand(1000,9999);
            $dataone['c_id'] = date('His',time()).$rand;//2018/05/30  name 乾坤
            $dataone['c_goodsid'] = $data['proid'];
            $dataone['c_start'] = $data['c_start'];
            $dataone['c_end'] = $data['c_end'];
            $dataone['c_plan'] = $data['c_plan'];
            $dataone['c_rebateid'] = $data['c_rebateid'];           
            $dataone['c_rmb'] = $data['c_rate'];
            $dataone['c_price'] = $yuanjia['g_price'];
            $dataone['c_money'] = $yuanjia['g_price'] - $data['c_rate'];
            $dataone['c_shopid'] = implode(" ",$shopid);
            $dataone['c_starttime'] = time();
            //判断店铺类型是否存在
            $storeid = Db::name('promotion')->where('c_shopid='.implode(" ",$shopid))->where('c_goodsid='.$data['proid'])->where('c_rebateid='.$data['c_rebateid'])->find();
            if($storeid == null){
            $result = Db::name('promotion')->insert($dataone);
            if($result){
                $this->success("添加成功", "blog/Promotion/prolist");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/zheKou");
            }
            }else{
                $this->success("该店铺类型商品已经添加过了!", "blog/Promotion/prolist");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    public function goodslist(){
        $where = [];
        $request = input('request.');
        if(!empty($request['g_goodsname'])){
            $keyword = $request['g_goodsname'];
            $where['g_goodsname'] = ['like',"%$keyword%"];
        }
		$adminid = cmf_get_current_admin_id();
		$shopid = Db::name("user")->where("id=".$adminid)->find();
        $list = Db::name('goods')->where($where)->where("storeid=".$shopid['store_id'])->order('g_ctime DESC')->paginate(15);
        $list->appends($request);//保持查询分页不变
        $this->assign('list',$list);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //修改促销数据
    public function zhekouUpdate(){
        $intNavId = $this->request->param("nav_id");
        $list = Db::name('rebate')->field('re_id,re_name')->select();
        $shuxing = Db::name('promotion')->where('c_id='.$intNavId)->find();
        $guige = Db::name('format')->where('id='.$shuxing['c_goodsid'])->select();
        $this->assign('guige',$guige);
        $this->assign('list',$list);
        $this->assign('intNavId',$intNavId);
        $listone = Db::name('store_type')->field('id,cat_name')->select(); 
        $this->assign('listone',$listone);
        return $this->fetch();
    }
    //修改促销数据，提交到数据库
    public function zheupdatePost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $por = Db::name('promotion')->where('c_id='.$data['c_id'])->find();//商品的id,价格
            //$yunjia = Db::name('format')->where('pid='.$por['c_goodsid'])->where('id='.$data['c_format'])->find();//查询选择规格的原价
            $datatwo = array();
            $datatwo['c_plan'] = $data['c_plan'];
            $datatwo['c_start'] = $data['c_start'];
            $datatwo['c_end'] = $data['c_end'];
            $datatwo['c_rebateid'] = $data['c_rebateid'];
            $datatwo['c_rmb'] = $data['c_rate'];//优惠金额
            $datatwo['c_money'] = $por['c_price'] - $data['c_rate'];
            $datatwo['store_type'] = $data['storetype'];
            $result = Db::name('promotion')->where('c_id='.$data['c_id'])->update($datatwo);
            if($result){
                $this->success("修改成功");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/zhekouUpdate");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //删除促销数据
    public function zhedeletePost(){
        $intNavId = $this->request->param("nav_id");
        $result = Db::name('promotion')->where('c_id='.$intNavId)->delete();
        if($result){
            $this->success("删除成功", "blog/Promotion/prolist");
        }else{
            $this->error('删除失败', "blog/Promotion/prolist");
        }
    }
    /*---------------------------------------------------------------------------------------*/
    //套餐列表
    public function setmealist(){
        $keywordComplex = [];
        $request = input('request.');
        if(!empty($request['keyword'])){
            $keyword = $request['keyword'];
            $keywordComplex['store_name|set_name'] = ['like',"%$keyword%"];
        }
        $list = Db::name('setmeal')->whereOr($keywordComplex)->alias('a')->join('__STORE__ b','a.set_shopid=b.id','left')->order('set_id DESC')->field('a.*,b.store_name')->paginate(10);
        $list->appends($request);//保持查询分页不变
        $this->assign('list',$list);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //显示添加套餐页面
    public function setaddlist(){
        return $this->fetch();
    }
    public function goodsmeal(){
        $where = [];
        $request = input('request.');
        if(!empty($request['g_goodsname'])){
            $keyword = $request['g_goodsname'];
            $where['g_goodsname'] = ['like',"%$keyword%"];
        }
        $adminid = cmf_get_current_admin_id();
        $shopid = Db::name('user')->where('id='.$adminid)->find();//店铺id
        //$goodsid = Db::name('store')->where('id='.$shopid['store_id'])->find();
        //$gooid = explode(",",$goodsid['cat_ware']);//把字符串拼装成数组
        $list = Db::name('goods')->where($where)->where('storeid='.$shopid['store_id'])->order('g_ctime DESC')->paginate(10);
        $list->appends($request);//保持查询分页不变
        $this->assign('list',$list);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //套餐接收添加数据
    public function setmealPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            /*$goodsid = explode(",",$data['proid'],-1);//把字符串拼装成数组去掉最后一位
            $goodslist = Db::name('goods')->where('g_id','in',$goodsid)->sum("g_price");
            $adminid = cmf_get_current_admin_id();
            $shopid = Db::name('user')->where('id='.$adminid)->field('store_id')->find();//查询当前用户所在的店铺id
            $datato = array();
            $datato['set_start']  = $data['set_start'];
            $datato['set_end']  = $data['set_end'];
            $datato['set_name']  = $data['set_name'];
            $datato['set_pricet']  = $data['set_pricet'];
            $datato['set_price'] = intval($goodslist);
            $datato['set_prices'] = intval($goodslist) - intval($data['set_pricet']);
            $datato['set_shopid'] = implode(" ",$shopid);
            $datato['set_goodsid'] = $data['proid'];
            $datato['set_startime'] = time();
            $result = Db::name('setmeal')->insert($datato);
            if($result){
                $this->success("添加成功", "blog/Promotion/setmealist");
            }else{
                $this->error("添加失败", "blog/Promotion/goodsmeal");
            }
        }else{
            $this->error(lang('ERROR'));*/
            $goodsid = explode(",",$data['proid'],-1);//把字符串拼装成数组去掉最后一位
            $goodslist = Db::name('goods')->where('g_id','in',$goodsid)->sum("g_price");
            $adminid = cmf_get_current_admin_id();
            $shopid = Db::name('user')->where('id='.$adminid)->field('store_id')->find();//查询当前用户所在的店铺id
            $datato = array();
            $rand = rand(1000,9999);
            $datato['set_id']  = date('His',time()).$rand;//2018/05/30  name 乾坤
            $datato['set_start']  = $data['set_start'];
            $datato['set_end']  = $data['set_end'];
            $datato['set_name']  = $data['set_name'];
            $datato['set_price'] = $goodslist;
            $datato['set_pricet']  = $data['set_pricet'];
            $datato['set_prices'] = $goodslist - $data['set_pricet'];
            $datato['set_goodsid'] = $data['proid'];
            $datato['set_shopid'] = implode(" ",$shopid);
            $datato['set_startime'] = time();
            $result = Db::name('setmeal')->insert($datato);
            if($result){
                $this->success("添加成功", "blog/Promotion/setmealist");
            }else{
                $this->error("添加失败", "blog/Promotion/goodsmeal");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //选择你添加商品的规格类型
    public function guionelist(){
        $intNavId = $this->request->param("nav_id");
        $goodid = Db::name('setmeal')->where('set_id='.$intNavId)->find();
        $gooid = explode(",",$goodid['set_goodsid'],-1);
        $list = Db::name('format')->where('pid','in',$gooid)->alias('a')->join('__GOODS__ b','a.pid=b.g_id','left')->field('a.*,b.g_goodsname')->order('ctime DESC')->paginate(100);
        $this->assign('list',$list);
        $this->assign('intNavId',$intNavId);
        return $this->fetch();
    }
    //添加选择规格数据
    public function guigepost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $datato = array();
            $jiage = Db::name('format')->where('id','in',$data['guigeid'])->sum('price');
            $youhui = Db::name('setmeal')->where('set_id='.$data['setmealid'])->find();
            $datato['set_format'] = implode(",", $data['guigeid']);
            $datato['set_price'] = intval($jiage);
            $datato['set_prices'] = intval($jiage) - $youhui['set_pricet'];
            $result = Db::name('setmeal')->where('set_id='.$data['setmealid'])->update($datato);
            if($result){
                $this->success("添加成功", "blog/Promotion/setmealist");
            }else{
                $this->error("添加失败", "blog/Promotion/guionelist");
            }
        }else{
             $this->error(lang('ERROR'));
        }
    }
    //修改套餐数据
    public function setkouUpdate(){
        $intNavId = $this->request->param("nav_id");
        $goodid = Db::name('setmeal')->where('set_id='.$intNavId)->find();
        $gooid = explode(",",$goodid['set_goodsid'],-1);
        $list = Db::name('format')->where('id','in',$gooid)->alias('a')->join('__GOODS__ b','a.id=b.g_id','left')->field('a.*,b.g_goodsname')->order('ctime DESC')->paginate(100);
        $this->assign('list',$list);
        $this->assign('intNavId',$intNavId);
        return $this->fetch();
    }
    //修改套餐数据，提交到数据库
    public function setmalPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            /*$goodsid = explode(",",$data['proid'],-1);//把字符串拼装成数组去掉最后一位
            $goodslist = Db::name('goods')->where('g_id','in',$goodsid)->sum("g_price");
            $datatwo = array();
            $datatwo['set_start'] = $data['set_start'];
            $datatwo['set_end'] = $data['set_end'];
            $datatwo['set_name'] = $data['set_name'];
            $datatwo['set_price'] = intval($goodslist);
            $datatwo['set_pricet'] = $data['set_pricet'];
            $datatwo['set_prices'] = intval($goodslist) - intval($data['set_pricet']);
            $datatwo['set_goodsid'] = $data['proid'];
            $datatwo['set_startime'] = time();
            $result = Db::name('setmeal')->where('set_id='.$data['set_id'])->update($datatwo);
            if($result){
                $this->success("修改成功");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/setmealist");
            }
        }else{
            $this->error(lang('ERROR'));*/
            //$jiage = Db::name('format')->where('id','in',$data['guigeid'])->sum('price');//总额
            $jiage = Db::name("setmeal")->where('set_id='.$data['set_id'])->find();
            $datatwo = array();
            $datatwo['set_name'] = $data['set_name'];
            $datatwo['set_start'] = $data['set_start'];
            $datatwo['set_end'] = $data['set_end'];
            $datatwo['set_pricet'] = $data['set_pricet'];//优惠金额
            $datatwo['set_prices'] = intval($jiage['set_price']) - $data['set_pricet'];
            $result = Db::name('setmeal')->where('set_id='.$data['set_id'])->update($datatwo);
            if($result){
                $this->success("修改成功");
            }else{
                $this->error(lang('EDIT_SUCCESS'), "blog/Promotion/setmealist");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //删除套餐数据
    public function setDelete(){
        $intNavId = $this->request->param("nav_id");
        $result = Db::name('setmeal')->where('set_id='.$intNavId)->delete();
        if($result){
            $this->success("删除成功", "blog/Promotion/setmealist");
        }else{
            $this->error('删除失败', "blog/Promotion/setmealist");
        }
    }
}

